//
//  OrderListViewController.m
//  Recipe
//
//  Created by bing han on 12-7-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "OrderListViewController.h"
#import "OrderListCell.h"
#import "OrderList.h"
#import "DBOptions.h"
#import "OrderRequire.h"

@interface OrderListViewController ()

@end

@implementation OrderListViewController
@synthesize dishercount = _dishercount, pricesum = _pricesum;
@synthesize orderDetailList = _orderDetailList;
@synthesize table = _table;
@synthesize backBtn = _backBtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [self initOrderList];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(IBAction)doBack:(id)sender{
    _backBtn.enabled = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) initOrderList{
    OrderList *ol = [OrderList newInstance];
    OrderRequire *or = [OrderRequire newInstance];
    
    DBOptions *dbOption = [DBOptions newInstence];
    FMDatabase *fmdb = dbOption.fmdatabase;
    int dishersum = 0;
    int allprice = 0;
    _orderDetailList = [[NSMutableArray alloc] init];
    NSArray *orderKeyArray = [ol.disherOrder allKeys];
    for (int i = 0 ; i< [orderKeyArray count]; i++) {
        int tmpprice = 0;
        NSMutableDictionary *disherDetail = [[NSMutableDictionary alloc] init];
        
        NSString *indexstr = [NSString stringWithFormat:@"%d",i+1];
        [disherDetail setObject:indexstr forKey:@"index"];
        NSString *orderBusinessKey = [orderKeyArray objectAtIndex:i];
        [disherDetail setObject:orderBusinessKey forKey:@"bussinesskey"];
        FMResultSet *rsdisher = [fmdb executeQuery:@"select cnamecn,cprice from dishes where businesskey=?",orderBusinessKey];
        while ([rsdisher next]) {
            //            NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
            [disherDetail setValue:[rsdisher objectForColumnName:@"cnamecn"] forKey:@"cnamecn"];
            [disherDetail setValue:[rsdisher objectForColumnName:@"cprice"] forKey:@"cprice"];
            NSString *tmppricestr = [rsdisher stringForColumn:@"cprice"];
            tmpprice = [tmppricestr intValue];
            //            [result release];
        }
        
        NSString *tmprequire = [or.disherRequire objectForKey:orderBusinessKey] == nil ? @"" :[or.disherRequire objectForKey:orderBusinessKey];
        
        [disherDetail setObject:tmprequire forKey:@"require"];
        [disherDetail setObject:[ol.disherOrder objectForKey:orderBusinessKey] forKey:@"count"];
        dishersum += [[ol.disherOrder objectForKey:orderBusinessKey] intValue];
        
        [disherDetail setObject:@"例" forKey:@"unit"];
        tmpprice *= [[ol.disherOrder objectForKey:orderBusinessKey] intValue];
        allprice += tmpprice;
        [_orderDetailList addObject:disherDetail];
        [disherDetail release];
    }
    
    _dishercount.text = [NSString stringWithFormat:@"%i",dishersum];
    _pricesum.text = [NSString stringWithFormat:@"%i",allprice];
    
    [dbOption closeDatabase];
}

#pragma mark --
#pragma mark OrderListDelegate
-(void) addDisher:(NSInteger)index{
    NSDictionary *tmpDict = [_orderDetailList objectAtIndex:index];
    NSString *tmpDisherKey = [tmpDict objectForKey:@"bussinesskey"];
    [self updateOrderList:1 bussinessKey:tmpDisherKey];
    [self initOrderList];
    [_table reloadData];
}

-(void) removeDisher:(NSInteger)index{
    NSDictionary *tmpDict = [_orderDetailList objectAtIndex:index];
    NSString *tmpDisherKey = [tmpDict objectForKey:@"bussinesskey"];
    [self updateOrderList:-1 bussinessKey:tmpDisherKey];
    [self initOrderList];
    [_table reloadData];
}

-(void) deleteDisher:(NSInteger)index{
    NSDictionary *tmpDict = [_orderDetailList objectAtIndex:index];
    NSString *tmpDisherKey = [tmpDict objectForKey:@"bussinesskey"];
    OrderList *ol = [OrderList newInstance];
    [ol.disherOrder removeObjectForKey:tmpDisherKey];
    [self initOrderList];
    [_table reloadData];
}

//更新系统点菜列表
-(void) updateOrderList:(NSInteger) value bussinessKey:(NSString *) key{
    OrderList *ol = [OrderList newInstance];
    if (value > 0) {
        [ol addDisher:key];
    }else{
        [ol substractDisher:key];
    }    
}

#pragma 
#pragma mark tableview delegate
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_orderDetailList count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId=@"editcell";
    
    OrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderListCell" owner:self options:nil] lastObject];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    cell.delegate = self;
    [cell setCellWithDictionary:[_orderDetailList objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
        return 60;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    _dishercount = nil;
    _pricesum = nil;
    _table = nil;
    _dishercount = nil;
    _pricesum = nil;
    _orderDetailList = nil;
    _backBtn = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

@end
